﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do2_PingPongColor : MonoBehaviour
{
    public Color[] colors;
    private MeshRenderer mrenderer;
    private int index = 0;
    private int dir = 1;
    private void Start()
    {
        //获取渲染器组件
        mrenderer = GetComponent<MeshRenderer>();
        //设置初始颜色
        mrenderer.material.color = colors[index];
    }

    //被点击
    private void OnMouseDown()
    {
        index += dir;
        #region 循环1
        // if (index == colors.Length)
        // {
        //     index = 0;
        // }
        #endregion 循环1

        #region 循环2
        //index = index % colors.Length;
        #endregion 循环2
        mrenderer.material.color = colors[index];

        #region PingPoing
        //如果已经是最大或者最小索引,dir就换个方向
        if (index == colors.Length - 1 || index == 0)
        {
            dir *= -1;
        }
        #endregion PingPoing
    }
}